package com.zjb.datastructure.sort;

import java.util.TooManyListenersException;

public class SelectionSort {

    static void sort(int[] datas) {
        for (int i = 0; i < datas.length-1; i++) {
            int min = datas[i];
            int j = i + 1;
            int temp = i;//辅助 ，如果temp==i 证明min没变，不需要移动
            for (; j < datas.length; j++) {
                if (min > datas[j]) {
                    min = datas[j];
                    temp = j;
                }
            }
            if (i == temp) {
                continue;
            }
            //把temp和i位置上的互换
            datas[temp] = datas[i];
            datas[i] = min;
        }
    }

    public static void main(String[] args) {
        int[] a = {1, 3, 4, 2, 1, 5};
        sort(a);
        for (int i : a) {

            System.out.print(i);
        }
    }
}
